import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/login/page/user_manager.dart';
import 'package:ls_work/ui/send_order/select_employee_page.dart';
import 'package:ls_work/util/utils.dart';

import '../../model/work_schedule/work_schedule_model.dart';

class WorkSchedulingInfoViewModel extends BaseViewModel {
  WorkSchedulingInfoViewModel(BuildContext context) : super(context);

  var format = ["yyyy", '年', 'm', '月'];
  var monthDate = DateTime.now();

  String get monthStr => monthDate.toDateStr(format);

  String get month => monthDate.toDateStr(['yyyy', '-', 'mm']);
  List<SchedulingDate> dateList = [];

  SchedulingDate? chooseDate;
  SchedulingDate? today;

  var model = WorkScheduleModel();

  dynamic humanId = UserManager.humanId;
  String userName = UserManager.userName;

  @override
  void onCreated() {
    super.onCreated();
    gridDate();
    notifyListeners();
  }

  void backClick() {
    var date = DateTime(monthDate.year, monthDate.month - 1, 1);
    monthDate = date;
    gridDate();
    notifyListeners();
  }

  void nextClick() {
    var date = DateTime(monthDate.year, monthDate.month + 1, 1);
    monthDate = date;
    gridDate();
    notifyListeners();
  }

  void gridDate() {
    var list = <SchedulingDate>[];
    var firstDate = DateTime(monthDate.year, monthDate.month, 1);
    var nextMonthFirstDate = DateTime(firstDate.year, firstDate.month + 1, 1);
    var lastDate = nextMonthFirstDate.add(Duration(days: -1));

    for (var i = 0; i < lastDate.day; i++) {
      var date = DateTime(firstDate.year, firstDate.month, firstDate.day + i);
      var schedulingDate = SchedulingDate(date, inMonth: true);
      list.add(schedulingDate);
    }

    for (var i = 1; i < firstDate.weekday; i++) {
      var date = DateTime(firstDate.year, firstDate.month, firstDate.day - i);
      var schedulingDate = SchedulingDate(date, inMonth: false);
      list.insert(0, schedulingDate);
    }
    print(firstDate.weekday);

    for (var i = 0; i < 7 - lastDate.weekday; i++) {
      var date =
          DateTime(firstDate.year, firstDate.month + 1, firstDate.day + i);
      var schedulingDate = SchedulingDate(date, inMonth: false);
      list.add(schedulingDate);
    }
    dateList = list;
    if (today == null) {
      today = dateList
          .firstWhere((e) => e.dateStr == DateTime.now().toDateStr(e._format));
      chooseDate = today;
    }
    getPlanData();
  }

  Future<void> getPlanData() async {
    showLoadingDialog();
    try {
      var list = await model.getPlanDetail(month, humanId);
      hideDialog();
      for (var date in dateList) {
        date.scheduleStatus = list[date.dateStr]??"";

      }
      notifyListeners();
    } catch (e) {
      hideDialog();
      showToast(NetErrorUtils.getNetError(e));
    }
  }

  Future<void> chooseStaff() async {
      var data = await Go().push(SelectEmployeePage());
      if(data!=null){
        userName = data['employeeName'];
        humanId = (data['employeeId'] as String).toInt();
        notifyListeners();
        getPlanData();
      }
  }
}

class SchedulingDate {
  var _format = ["yyyy", '-', 'mm', '-', 'dd'];
  String? scheduleStatus;

  DateTime? dateTime;

  bool inMonth;

  SchedulingDate(this.dateTime, {this.inMonth = false});

  String? get dateStr => dateTime?.toDateStr(_format);

  String? get day => dateTime?.day.toString();
}
